package com.dxw.web;

import com.dxw.dao.BaseDao;
import com.dxw.entity.Student;
import com.dxw.utils.ExcelUtils;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

@WebServlet("/excel")
@MultipartConfig
public class ExcelServlet extends HttpServlet {
    private BaseDao bd = new BaseDao();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String m = request.getParameter("m");
        switch(m){
            case "show":show(request,response);break;
            case "exportExcel" : exportExcel(request, response);break;
            case "importExcel" : importExcel(request, response);break;
        }
    }

    private void importExcel(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        //拿到文件
        Part excelFile = request.getPart("excelFile");
        Object[][] params = ExcelUtils.importExcelXlsx(excelFile.getInputStream());
        String sql = "insert into student values(?,?,?,?,?,?)";
        int[] ints = bd.batch(sql, params);
        response.getWriter().write("成功导入" + ints.length + "条数据");
    }


    private void exportExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String sql = "select * from student";
        List<Student> students = bd.queryObjects(sql,Student.class);
        //获取我们的导出报表的流
        InputStream in = ExcelUtils.exportExcelXlsx(students);
        //获取输出流，不能使用文件输出流
        // 写入字节之前，我们需要设计一个设计的响应头
        //证明这个流文件的是一个附件
        String filename = URLEncoder.encode("学生信息.xlsx", "UTF-8");
        response.addHeader("content-disposition", "attachment;filename=" + filename);
        //响应头的输出流
        ServletOutputStream out = response.getOutputStream();
        //创建一个字节
        byte[] b = new byte[1024];
        int len;
        while ((len=in.read(b))!=-1){
            //读取b,从0->（输入流）读取b的长度
            out.write(b,0,len);
        }
        //释放资源
        out.flush();
        out.close();
        in.close();
        response.flushBuffer();;
    }

    private void show(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String sql = "select * from student";
        List<Student> students = bd.queryObjects(sql,Student.class);
        //将我们得到的对象设置到域中
        request.setAttribute("students",students);
        //转发界面
        request.getRequestDispatcher("/index.jsp").forward(request,response);
    }


}
